<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="__PUBLIC__/layui/css/layui.css"  media="all">
    <link rel="stylesheet" href="__PUBLIC__/font-awesome/css/font-awesome.min.css" media="all" />
    <link rel="stylesheet" href="__CSS__/admin.css"  media="all">
    <!-- 注意：如果你直接复制所有代码到本地，上述css路径需要改成你本地的 -->
    <style type="text/css">

        /* tooltip */
        #tooltip{
            position:absolute;
            border:1px solid #ccc;
            background:#333;
            padding:2px;
            display:none;
            color:#fff;
        }
    </style>
</head>
<body style="margin: 0 20px">

<table class="layui-table" lay-size="sm">
    <colgroup>
        <col width="50">
        <col width="70">
        <col width="100">
        <col width="50">
        <col width="50">
        <col width="50">
        <col width="150">
        <col width="150">
        <col width="100">
        <col width="50">
    </colgroup>
    <thead>
    <tr style="height:70px;border-top:3px solid;border-left:3px solid;border-right:3px solid;">
        <th>时间</th>
        <th>城市</th>
        <th>行程</th>
        <th>距离</th>
        <th>预计时长</th>
        <th>基础价格</th>
        <th>额外价格</th>
        <th>票务价格</th>
        <th>补充</th>
        <th>当日总价</th>
    </tr>
    </thead>
    <tbody>
    {volist name="priceData" id="vo"}
    {if $vo.a_num=='0'}
    <tr style="height:70px;border-top:3px solid;border-left:3px solid;border-right:3px solid;">
        <td>{$vo.time}</td>
        <td>无</td>
        <td>无</td>
        <td>0</td>
        <td>0</td>
        <td>0</td>
        <td></td>
        <td></td>

        <td>
            <input type="hidden" class="original_totalPrice" value="0">
            <input type="hidden" class="activity_id" value="{$vo.id}">
            <input type="hidden" class="changePrice" value="{$vo.changePrice}">
            <input type="hidden" class="totalPrice" value="{$vo.totalPrice}">
            <input placeholder="请输入调整金额" value="{$vo.changePrice}" autocomplete="off" class="layui-input changeNum" type="number" oninput="if(value.length>5)value=value.slice(0,6)">
        </td>
        <td class="totalPrice_td">
            0
        </td>
    </tr>
    {else}
    {volist name="vo.activities" key="key1" id="vo1"}
    <tr style="{if $key1=='1'}height:70px;border-top:3px solid;border-left:3px solid;border-right:3px solid;{/if}">
        {if $key1=="1"}
        <td rowspan="{$vo.a_num}">{$vo.time}</td>
        {/if}
        <td>{$vo1.cityStr}</td>
        <td>{$vo1.displayString}</td>
        <td>{if !empty($vo1.distance)}{$vo1.distance} km{else}0{/if}</td>
        <td>{if !empty($vo1.duration)}{$vo1.duration} 小时{else}0{/if}</td>
        <td>￥{$vo1.baseAmount}</td>
        <td>
            {if !empty($vo1.extra.exceedKMFee)}<br>超出基础里程费用 = ￥{$vo1.extra.exceedKMFee}<br>{/if}
            {if !empty($vo1.extra.overtimeFee)}超时费 = ￥{$vo1.extra.overtimeFee}<br>{/if}
            {if !empty($vo1.extra.hotelFee)}司导酒店费用 = ￥{$vo1.extra.hotelFee}<br>{/if}
            {if !empty($vo1.extra.dinnerFee)}餐补 = ￥{$vo1.extra.dinnerFee}<br>{/if}
            {if !empty($vo1.extra.oilFee)}油费 = ￥{$vo1.extra.oilFee}<br>{/if}
            {if !empty($vo1.extra.parkingFee)}停车费 = ￥{$vo1.extra.parkingFee}<br>{/if}
            {if !empty($vo1.extra.nightFee)}夜间服务费 = ￥{$vo1.extra.nightFee}<br>{/if}
            {if !empty($vo1.extra.assistanceFee)}陪护费用，导游费用 = ￥{$vo1.extra.assistanceFee}<br>{/if}
            {if !empty($vo1.extra.childSeatFee)}儿童座椅 = ￥{$vo1.extra.childSeatFee}<br>{/if}
        </td>
        <td>
            <br>{$vo1.tickitStr}<br>
            <br>{$vo1.goodsStr}<br>
        </td>

        {if $key1=="1"}
        <td rowspan="{$vo.a_num}">
            <input type="hidden" class="original_totalPrice" value="{$vo.totalPrice}">
            <input type="hidden" class="activity_id" value="{$vo.id}">
            <input type="hidden" class="changePrice" value="{$vo.changePrice}">
            <input type="hidden" class="totalPrice" value="{$vo.totalPrice}">
            <input placeholder="请输入调整金额" value="{$vo.changePrice}" autocomplete="off" class="layui-input changeNum" type="number" oninput="if(value.length>5)value=value.slice(0,6)">
        </td>
        <td rowspan="{$vo.a_num}" class="totalPrice_td">
            <br>
            <br>
            ￥{$vo.totalPrice}
            <br>
            <br>
            美元：${$vo.totalPrice_USD}
            <br>
            <br>
            加元：${$vo.totalPrice_CAD}
            <br>
            <br>
        </td>
        {/if}
    </tr>
    {/volist}
    {/if}
    {/volist}

    <tr>
        <td colspan="10" style="height:50px;border-top:3px solid;"></td>
    </tr>

    <tr style="height:70px;border:3px solid;text-align: center;">
        <td>
            行程总价
            <input type="hidden" id="tripTotlePrice">
        </td>
        <td colspan="6" id="priceExpression"></td>
        <td>
            <input placeholder="请输入利润系数" {notempty name="$ratio"}value="{$ratio}"{/notempty} name="ratio" autocomplete="off" class="layui-input" type="text" id="ratio">
        </td>
        <td colspan="2" id="tripPrice"></td>
    </tr>
    </tbody>
</table>

<div class="layui-input-block" style="text-align: center;margin-left:-20%;">
    <input type="hidden" id="trip_id" value="{$trip_id}">
    <input type="hidden" id="ex_USD" value="{$ex_USD}">
    <input type="hidden" id="ex_CAD" value="{$ex_CAD}">
    <button class="layui-btn" id="save">确定</button>
</div>

<div style="height:50px;"></div>


<script src="__PUBLIC__/layui/layui.js" charset="utf-8"></script>
<script src="__PUBLIC__/jquery/jquery.min.js"></script>
<script>
    var message;
    layui.config({
        base: '__JS__/',
        version: '1.0.1'
    }).use(['app', 'message'], function() {
        var app = layui.app,
            $ = layui.jquery,
            layer = layui.layer;
        //将message设置为全局以便子页面调用
        message = layui.message;
        //主入口
        app.set({
            type: 'iframe'
        }).init();
    });
</script>

<script>
    //验证是否为数字
    function isNumber(value) {
        var patrn = /^(-)?\d+(\.\d+)?$/;
        if (patrn.exec(value) == null || value == "") {
            return false;
        } else {
            return true;
        }
    }

    $(document).on("input propertychange",".changeNum",function(){
        var value = $(this).val();
        var totlePrice = $(this).siblings(".original_totalPrice").val();

        if(value=='' || value==0){
            $(this).siblings(".changePrice").attr("value",0);
        }

        if(isNumber(value)){
            totlePrice = Number(totlePrice) + Number(value);
            $(this).siblings(".changePrice").attr("value",value);
        }
        if(totlePrice<0){
            totlePrice = 0;
        }

        var totlePrice_html = "<br><br>￥"+totlePrice;

        var ex_USD = $("#ex_USD").attr("value");
        var ex_CAD = $("#ex_CAD").attr("value");

        ex_USD = parseFloat(ex_USD);
        ex_CAD = parseFloat(ex_CAD);

        var totalPrice_USD = Math.ceil(totlePrice/ex_USD);
        var totalPrice_CAD = Math.ceil(totlePrice/ex_CAD);

        totlePrice_html += "<br><br>美元：$"+totalPrice_USD+"<br><br>加元：$"+totalPrice_CAD+"<br><br>";

        $(this).siblings(".totalPrice").attr("value",totlePrice);
        $(this).parent().parent().find(".totalPrice_td").html(totlePrice_html);

        priceExpression();

        return false;
    });


    $(document).on("input propertychange","#ratio",function(){
        priceExpression();
        return false;
    });


    //计算行程总价
    function priceExpression(){
        var dayNum = 1;
        var expressionStr = '';
        var tripPrice = 0;
        var ratio = $("#ratio").val();
        $(".totalPrice").each(function(){
            var dayPrice = $(this).attr("value");
            dayPrice = Number(dayPrice);
            if(dayPrice>0){
                if(expressionStr == ''){
                    expressionStr += "￥" + dayPrice;
                }else{
                    expressionStr += " &nbsp;&nbsp; + &nbsp; " + "￥" + dayPrice;
                }

                tripPrice += dayPrice;
            }
            dayNum++;
        });

        if(ratio>0){
            tripPrice = Math.ceil(ratio*tripPrice);
        }

        if(expressionStr!==''){
//            expressionStr += " &nbsp;&nbsp; = &nbsp; " + "￥"+tripPrice;
            expressionStr += " &nbsp;&nbsp; = &nbsp; ";
        }

        $("#tripTotlePrice").attr("value",tripPrice);
        $("#priceExpression").html(expressionStr);
        $("#tripPrice").html("￥"+tripPrice);

        return false;
    }


    $("#save").click(function(){
        var str = '';
        var p_str = '';
        var tripTotlePrice = $("#tripTotlePrice").attr("value");
        var ratio = $("#ratio").val();
        var trip_id = $("#trip_id").attr("value");
        $(".changePrice").each(function(){
            var changePriceValue = $(this).attr("value");
            var id = $(this).siblings(".activity_id").attr("value");
            var dayTotalPrice = $(this).siblings(".totalPrice").attr("value");

            if(changePriceValue==''){
                changePriceValue = 0;
            }

            if(dayTotalPrice=='' || dayTotalPrice<=0){
                dayTotalPrice = 0;
            }

            str += id+"_"+changePriceValue + ",";
            p_str += id+"_"+dayTotalPrice + ",";
        });

        $.ajax({
            url:"{:url('admin/trip/saveChangePrice')}",
            type:"post",
            data:{str:str,p_str:p_str,trip_id:trip_id,ratio:ratio},
            success:function(res) {
                layer.msg(res.msg);
                setTimeout(function(){
                    var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                    parent.layer.close(index); //再执行关闭
                },1500)
            }
        })
        return false;
    });



    priceExpression();

</script>
</body>
</html>
